const { ccclass, property } = cc._decorator;
import frameworkManager from "./frameworkManager";
import EngineUtil from "./EngineUtil";
import UiManage from "./UiManage";

@ccclass
export default class DrillFillInInPutCtrl extends cc.Component {
    @property(cc.EditBox)
    hp_input = null;
    @property(cc.EditBox)
    color_input = null;
    @property(cc.EditBox)
    dir_input = null;
    @property(cc.Node)
    btn_close = null;
    @property(cc.Node)
    btn_save = null;

    initData = null;

    onLoad() {
        this.addButtonListen();
        this.initData = null;
    }

    addButtonListen() {
        UiManage.addButtonListen(this.btn_close, this.clickClose, this);
        UiManage.addButtonListen(this.btn_save, this.clickSave, this);
    }

    clickClose() {
        this.hp_input.getComponent(cc.EditBox).string = "";
        this.color_input.getComponent(cc.EditBox).string = "";
        this.dir_input.getComponent(cc.EditBox).string = "";
        this.node.active = false;
    }

    clickSave() {
        const hp = this.hp_input.getComponent(cc.EditBox).string;
        const color = this.color_input.getComponent(cc.EditBox).string;
        const dir = this.dir_input.getComponent(cc.EditBox).string;

        if (hp && parseInt(hp) !== 0) {
            if (color && parseInt(color) !== 0) {
                if (dir) {
                    this.initData = {
                        hp: parseInt(hp),
                        color: parseInt(color),
                        dir: parseInt(dir)
                    };
                    frameworkManager.log("钻头的初始化数据======", this.initData);
                    this.clickClose();
                } else {
                    EngineUtil.showManageViewToast("方向输入不正确");
                }
            } else {
                EngineUtil.showManageViewToast("颜色输入不正确请检查");
            }
        } else {
            EngineUtil.showManageViewToast("血量输入不正确请检查");
        }
    }

    getDrillfillData() {
        return this.initData;
    }
}